<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
    <title>芳小微宣</title>
    <script src="js/jquery.min.js"> </script>
    <script src="js/index.js"></script>
    <link rel="stylesheet" href="css/index.css">
</head>
<body>
<div style="margin-bottom: 70px;">
    <div style="background: url('images/honorBg.png'); background-size: 100% 100%;background-repeat: no-repeat;">
        <div style="width: 30%;float: left;margin: 3rem 5%;">
              <img id="studentImg" src="" style="width: 100%;border-radius: 55px;"/>
        </div>
        <div style="width: 60%;float: left; margin: 3rem 0;font-size: 6rem;color: #fff;">
            <ul>
                <li>姓名：<span id="name"></span></li>
                <li>学号：<span id="schoolNum"></span></li>
                <li>班级：<span id="className"></span></li>
                <li>获赞数：<span id="goodNum"></span></li>
            </ul>
        </div>
        <div style="clear: both"></div>
    </div>
    <div id="honorData">
    </div>
</div>
<div class="fixDiv">
   <div id="chooseSub">
       选择提交
   </div>
</div>
<script>
$(function(){
    $.ajax({
        type : "POST",
        url :  path + "/api/weixin/getStudentHonnor?token=" + window.localStorage.getItem("token"),
        data : {},
        dataType : 'json',
        success : function(msg) {
            var imghtml = "";
            for(var i=0;i<msg.honorTypeList.length;i++){
                imghtml += '<div class="honorDiv"><img src="images/dark.png" class="honorImg" style="width: 75%;" id="'+ msg.honorTypeList[i].id +'"/><br>'+ msg.honorTypeList[i].honorName +'</div>';
            }
            //设置中间  margin-left: 5%;margin-right: 5%;
            $("#honorData").append(imghtml + '<div style="clear: both"></div>');

            for(var k=0;k<15;k++){  //$(".honorDiv").length
                if((k%3)-1 == 0){
                    $(".honorDiv").eq(k).css("margin-left","5%");
                    $(".honorDiv").eq(k).css("margin-right","5%")
                }
            }
            $(".honorDiv").click(function(){
                if($(this).find("img")[0].src.indexOf("bright")>0){
                    $(this).find("img")[0].src = "images/dark.png";
                }else{
                    $(this).find("img")[0].src = "images/bright.png";
                }
            });
            $.ajax({
                type : "POST",
                url :  path + "/api/weixin/getHonorByStudentId",
                data : {
                    "studentId" : getParam("studentid"),
                    "token" : window.localStorage.getItem("token")
                },
                dataType : 'json',
                success : function(data) {
                    if(data.code == 0){
                        $("#name").html(data.studentInfoEntity.name);
                        $("#schoolNum").html(data.studentInfoEntity.studentId);
                        $("#className").html(data.studentInfoEntity.classId);
                        $("#goodNum").html(data.studentInfoEntity.honorStudentList.length);
                        if(!data.studentInfoEntity.img){
                            $("#studentImg").attr("src","images/demo.png")
                        }else{
                            $("#studentImg").attr("src",photoPath + data.studentInfoEntity.img);
                        }
                        for(var i=0;i<$(".honorImg").length;i++){
                            for(var j=0;j<data.studentInfoEntity.honorStudentList.length;j++){
                                if($(".honorImg").eq(i).attr("id") == data.studentInfoEntity.honorStudentList[j].honorId){
                                    $(".honorImg").eq(i).attr("src","images/bright.png");
                                    $(".honorImg").eq(i).parent().unbind("click");
                                }
                            }
                        }
                    }else if(msg.code == 501){
                        window.location.href = "login.html";
                    }
                }
            });
        }
    });
});
$("#chooseSub").click(function(){
    var honorId = [];
    for(var i=0;i<$(".honorImg").length;i++){
        if($(".honorImg").eq(i).attr("src").indexOf("bright")>0){
            honorId.push($(".honorImg").eq(i).attr("id"));
        }
    }
    $.ajax({
        type : "POST",
        url :  path + "/api/weixin/addHonorStudent",
        data : {
            "studentId" : getParam("studentid"),
            "classId" : $("#className").html,
            "honorIds":JSON.stringify(honorId),
            "token" : window.localStorage.getItem("token")
        },
        dataType : 'json',
        success : function(data) {
            if(data.code == 0){
                window.location.href = "chooseClass.html";
            }else if(msg.code == 501){
                window.location.href = "login.html";
            }
        }
    });
});

</script>
</body>
</html>